package com.homestay.controller;

import com.homestay.beans.Vip;
import com.homestay.exception.UserException;
import com.homestay.service.VipService;
import com.homestay.service.impl.VipServiceImpl;
import com.homestay.utils.FormBeanUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/vip") // 设置访问路径,必须和 jsp页面表单的 action属性保持一致
public class VipServlet extends HttpServlet {

    static VipService vipService = new VipServiceImpl();

    // 重写service方法
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 设置请求编码,防止乱码
        req.setCharacterEncoding("utf-8");
        // 设置响应内容类型,防止乱码
        resp.setContentType("text/html;charset=utf-8");

        String action = req.getParameter("action");
        switch (action) {
            case "add":
                vipAdd(req, resp);
                break;
            case "query":
                vipQuery(req, resp);
                break;
            case "delete":
                vipDelete(req, resp);
                break;
            case "goupdate":
                vipGoUpdate(req, resp);
                break;
            case "update":
                vipUpdate(req, resp);
                break;
            default:
                break;
        }
    }

    // 跳转到会员修改页面
    private static void vipGoUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Vip vip = vipService.queryById(id);
        // 将数据保存到request对象中
        req.setAttribute("vip", vip);
        // 请求转发数据，跳转到 vip_update.jsp页面
        req.getRequestDispatcher("/views/vip_update.jsp").forward(req, resp);
    }

    // 会员修改
    private void vipUpdate(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        // String id = req.getParameter("id");
        // String name = req.getParameter("name");
        // String sex = req.getParameter("sex");
        // String card = req.getParameter("card");
        // String phone = req.getParameter("phone");
        // String v_type = req.getParameter("v_type");
        // String startTime = req.getParameter("startTime");
        // String endTime = req.getParameter("endTime");
        // Vip vip = new Vip(id, name, sex, card, phone, v_type, startTime, endTime);

        // 使用工具类简化封装对象操作
        Vip vip = FormBeanUtils.populate(req, Vip.class);

        int result = vipService.update(vip);
        if (result > 0) {
            resp.getWriter().write("<script>alert('会员更新成功');location.href='/vip?action=query'</script>");
        } else {
            resp.getWriter().write("<script>alert('会员更新失败');location.href='/vip?action=update?id="+ vip.getId() +"'</script>");
        }
    }

    // 会员删除
    private void vipDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        try {
            int result = vipService.deleteById(id);
            if (result > 0) {
                resp.getWriter().write("<script>alert('删除成功');location.href='/vip?action=query'</script>");
            } else {
                resp.getWriter().write("<script>alert('删除失败');location.href='/vip?action=query'</script>");
            }
        } catch (UserException e) {
            resp.getWriter().write("<script>alert('"+ e.getMessage() +"');location.href='/vip?action=query'</script>");
        }
    }

    // 会员添加
    private static void vipAdd(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

//            String name = req.getParameter("name");
//            String sex = req.getParameter("sex");
//            String card = req.getParameter("card");
//            String phone = req.getParameter("phone");
//            String v_type = req.getParameter("v_type");
//            String startTime = req.getParameter("startTime");
//            String endTime = req.getParameter("endTime");
//            Vip vip = new Vip(name, sex, card, phone, v_type, startTime, endTime);
            // 使用工具类简化封装对象操作
            Vip vip = FormBeanUtils.populate(req, Vip.class);
        try {

            int result = vipService.add(vip);
            if (result > 0) {
                resp.getWriter().write("<script>alert('添加成功');location.href='/views/vip_add.jsp'</script>");
            } else {
                resp.getWriter().write("<script>alert('添加失败');location.href='/views/vip_add.jsp'</script>");
            }
        } catch (UserException e) {
            String message = e.getMessage();
            // 获取错误信息
            if("身份证号已经存在".equals(message)){
                req.setAttribute("errorCard", message);
                req.setAttribute("errorPhone", "");
            }else if("手机号已经存在".equals(message)){
                req.setAttribute("errorPhone", message);
                req.setAttribute("errorCard", "");
            }
            req.setAttribute("vip", vip);
            req.getRequestDispatcher("/views/vip_add.jsp").forward(req, resp);
        }
    }

    // 会员查询，根据姓名、省份证号、手机号查询
    private static void vipQuery(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

        String name = req.getParameter("name");
        String card = req.getParameter("card");
        String phone = req.getParameter("phone");

        if (name == null || name.isEmpty()) {
            name = "";
        }
        if (card == null || card.isEmpty()) {
            card = "";
        }
        if (phone == null || phone.isEmpty()) {
            phone = "";
        }
        List<Vip> vipList = vipService.query(name, card, phone);
        // 将数据保存到request对象中
        req.setAttribute("vips", vipList);
        req.setAttribute("name", name);
        req.setAttribute("card", card);
        req.setAttribute("phone", phone);
        // 请求转发数据，跳转到 vip_list.jsp页面
        req.getRequestDispatcher("/views/vip_list.jsp").forward(req, resp);
        //System.out.println("数据集合大小:"+vips.size());
    }



}
